﻿using System;
using System.Collections.Generic;
using DimensionsHelper.ExcelAddIn.Core;
using DimensionsHelper.ExcelAddIn.Core.Sql;
using DimensionsHelper.ExcelAddIn.Ms.Controls;

namespace DimensionsHelper.ExcelAddIn.Ms
{
    public partial class ThisAddIn
    {

        private static readonly Dictionary<Type, IExcelAddIn> Registry = new Dictionary<Type, IExcelAddIn>();

        private static void Register<T>() where T : IExcelAddIn, new()
        {
            Registry[typeof(T)] = new T();
        }


        public static T GetService<T>() where T : IExcelAddIn, new()
        {
            if (Registry.TryGetValue(typeof(T), out var service))
            {
                return (T)service;
            }

            throw new InvalidOperationException($"Service of type {typeof(T).Name} is not registered.");
        }


        // SettingWindow
        private static SettingWindow _settingWindow = null;


        public void ShowSettingWindow()
        {
            if (_settingWindow == null)
            {
                _settingWindow = new SettingWindow();
            }

            _settingWindow.ShowDialog();
        }


        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
            Register<ScriptBuilder>();
        }


        private void ThisAddIn_Shutdown(object sender, EventArgs e)
        {
        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            Startup += ThisAddIn_Startup;
            Shutdown += ThisAddIn_Shutdown;
        }
        
        #endregion
    }
}
